﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ZSens
{
    /// <summary>
    /// Interaction logic for ConfigButton.xaml
    /// </summary>
    public partial class ConfigButton : Window
    {
        private List<Key> down_keys;
        private bool isUp;
        private Button owner;
        public ConfigButton(Button w)
        {
            InitializeComponent();
            down_keys = new List<Key>();
            isUp = true;
            owner = w;
            PreviewKeyUp += ConfigButton_PreviewKeyUp;
            PreviewKeyDown += ConfigButton_PreviewKeyDown;
            buttonName.Text = owner.text;
        }

        void ConfigButton_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            isUp = true;
        }

        private void ConfigButton_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (isUp)
            {
                down_keys.Clear();
                tex_box.Text = "";
                isUp = false;
            }
            if (tex_box.IsFocused && !down_keys.Contains(e.Key))
            {
                if (down_keys.Count == 0)
                    tex_box.Text = e.Key.ToString();
                else
                    tex_box.Text += " + " + e.Key.ToString();
                down_keys.Add(e.Key);
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if ((bool)rdbtn_keyboard.IsChecked)
            {
                List<VMultiAPI.Keyboard.KeyCode> keys = new List<VMultiAPI.Keyboard.KeyCode>();
                foreach (Key key in down_keys)
                    keys.Add(VMultiAPI.Keyboard.vMultiKeyMap[(int)key]);

                
                owner.BindedAction = new List<Action>{
                   new PressKeyboardKeyAction(owner, keys)
                };
            }
            else if ((bool)rdbtn_joystick.IsChecked)
            {
                if (joystick_cbox.SelectedIndex != -1)
                {
                    owner.BindedAction = new List<Action>
                    {
                        new PressJoystickButtonAction(owner,new List<int>{joystick_cbox.SelectedIndex})
                    };
                }
            }
            owner.text = buttonName.Text;
            owner.DisplayString = owner.GetDisplayString();
           
            Close();
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            Close();
        }

    }
}
